package com.bac.rentmap.ad.flat.rating.update;

import java.util.Date;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;

import org.springframework.beans.factory.annotation.Required;

import com.bac.rentmap.ad.flat.FlatAd;
import com.bac.rentmap.ad.flat.image.FlatImageService;
import com.bac.rentmap.ad.flat.jdo.JdoFlatAdDao;
import com.bac.rentmap.ad.flat.rating.FlatAdRatingCalculator;
import com.bac.rentmap.ad.util.DateHelper;
import com.bac.rentmap.jdo.JdoSupport;

public class RatingAwareLastPublishingDateUpdater extends JdoSupport implements
        RatingAwareFlatAdUpdater {
    private static final Logger log = Logger
            .getLogger(RatingAwareLastPublishingDateUpdater.class.getName());

    private FlatAdRatingCalculator flatAdRatingCalculator;
    private DateHelper dateHelper;
    private JdoFlatAdDao flatAdDao;
    private FlatImageService flatImageService;

    @Override
    public FlatAd update(String secureId) {
        PersistenceManager persistenceManager = getPersistenceManager();
        try {
            FlatAd res = flatAdDao.loadBySecureId(secureId, persistenceManager);
            Date date = dateHelper.getDateForMidnight();
            res.setLastPublishingDate(date);
            flatImageService.updatePublishingDate(res, persistenceManager);

            long oldRating = res.getRating();
            long rating = flatAdRatingCalculator.calculateRating(res);
            log.info("Updating LastPublishing date to " + date
                    + " and rating to " + rating + " from " + oldRating);
            res.setRating(rating);
            return res;
        } finally {
            persistenceManager.close();
        }
    }

    @Override
    public RatingEstimation estimateRatingAfterUpdate(String secureId) {
        PersistenceManager persistenceManager = getPersistenceManager();
        FlatAd flatAd;
        try {
            flatAd = flatAdDao.loadBySecureId(secureId, persistenceManager);
        } finally {
            persistenceManager.close();
        }
        long oldRating = flatAd.getRating();
        Date date = dateHelper.getDateForMidnight();
        flatAd.setLastPublishingDate(date);

        long res = flatAdRatingCalculator.calculateRating(flatAd);
        log.info("Estimating rating: " + res
                + " after updating last publishing date to" + date);
        return new RatingEstimation(res, oldRating);
    }

    @Required
    public void setFlatAdRatingCalculator(
            FlatAdRatingCalculator flatAdRatingCalculator) {
        this.flatAdRatingCalculator = flatAdRatingCalculator;
    }

    @Required
    public void setDateHelper(DateHelper dateHelper) {
        this.dateHelper = dateHelper;
    }

    @Required
    public void setFlatAdDao(JdoFlatAdDao flatAdDao) {
        this.flatAdDao = flatAdDao;
    }

    @Required
    public void setFlatImageService(FlatImageService flatImageService) {
        this.flatImageService = flatImageService;
    }
}
